package com.epam.diplom.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.epam.diplom.bean.Client;
import com.epam.diplom.bean.view.ClientInList;
import com.epam.diplom.dao.ClientDao;
import com.epam.diplom.service.ClientService;

public class ClientServiceImpl implements ClientService {
	
	private ClientDao clientDao;
	
	@Transactional(readOnly = true)
	public List<ClientInList> getClients() {
		List<Client> dbClients = clientDao.getClients();
		List<ClientInList> viewClients = new ArrayList<ClientInList>(dbClients.size());
		for (Client client : dbClients) {
			viewClients.add(convertDbClient(client));
		}
		return viewClients;
	}

	private ClientInList convertDbClient(Client client) {
		ClientInList clientView = new ClientInList();
		clientView.setEmail(client.getEmail());
		clientView.setFirstName(client.getFirstName());
		clientView.setLastName(client.getLastName());
		clientView.setPatronymik(client.getPatronymik());
		clientView.setPhone(client.getPhone());
		return clientView;
	}
	
	@Autowired
	public void setClientDao(ClientDao clientDao) {
		this.clientDao = clientDao;
	}
}
